package com.sbj.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sbj.dto.req.BuyReq;
import com.sbj.dto.res.BuyRes;
import com.sbj.dto.res.GoodsRes;
import com.sbj.entity.BuyGoods;
import com.sbj.entity.Goods;
import com.sbj.service.GoodsService;
import com.sbj.service.IBuyGoodsService;
import com.zmh.framework.config.BaseCommonInterface;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author summer
 * @since 2024/08/10 10:02:39
 */
@RestController
@RequestMapping("/buyGoods")
@AllArgsConstructor
public class BuyGoodsController implements BaseCommonInterface<BuyReq, BuyRes> {

    private final IBuyGoodsService buyGoodsService;

    @Override
    public Result<String> save(@Valid BuyReq buyReq) {
        buyGoodsService.saveOrUpdate(Convert.convert(BuyGoods.class,buyReq));
        return Result.successResult();
    }

    @Override
    public Result<String> del(Integer id)
    {
        buyGoodsService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<BuyRes>> lists(BuyReq buyReq) {
        return Result.successResult(buyGoodsService.lists(buyReq));
    }

    @Override
    public Result<BuyRes> findById(Integer id) {
        return Result.successResult(Convert.convert(BuyRes.class,buyGoodsService.getById(id)));
    }

    @Override
    public Result<String> batchDel(List<BuyRes> list) {
        return null;
    }

    private final GoodsService goodsService;

    @GetMapping(value = "/categoryGoods/{categoryId}")
    public Result<List<GoodsRes>> categoryGoods(@PathVariable Integer categoryId){
        List<Goods> goods = goodsService.list(Wrappers.lambdaQuery(Goods.class).eq(Goods::getCategoryId, categoryId));
        return Result.successResult(Convert.toList(GoodsRes.class,goods));
    }

}
